package com.veterinaria.dao;

import java.io.Reader;
import java.util.ArrayList;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.veterinaria.bean.HCBean;
import com.veterinaria.bean.PacienteBean;
import com.veterinaria.services.HCService;

public class HCDAO implements HCService{
	
	SqlSessionFactory sqlmapper= null;{
	   try {	
	   	 Reader reader = Resources.getResourceAsReader("ConfiguracionIbatis.xml");
	   	 sqlmapper =new  SqlSessionFactoryBuilder().build(reader);
	   	 } catch (Exception e) {
			e.printStackTrace();
		}
	    }
	
	@Override
	public void grabarHC(HCBean obj) throws Exception {
		// TODO Auto-generated method stub
		SqlSession session = sqlmapper.openSession();
		String msg="";
		try {
			msg="exito"+ session.insert("hcxml.insertarHC",obj);
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
			msg=e.getMessage();
			session.rollback();
		}finally{
			session.close();
		}
	}

	@Override
	public ArrayList<HCBean> listarHC() throws Exception {
		// TODO Auto-generated method stub
		ArrayList<HCBean> lista = null;
		SqlSession session = sqlmapper.openSession();
		try {
			lista = (ArrayList<HCBean>)session.selectList("hcxml.listaHC");
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			session.close();
		}
		return lista;
	}

	@Override
	public HCBean obtenerHC(HCBean obj) throws Exception {
		// TODO Auto-generated method stub
		HCBean bean=null;
		SqlSession session = sqlmapper.openSession();			
		try {				
			bean= (HCBean) session.selectOne("hcxml.obtenerHC",obj);				
		} catch (Exception e) {
			e.printStackTrace();
		}finally{ session.close();}
		return bean;
	}

	@Override
	public void ModificarHC(HCBean obj) throws Exception {
		// TODO Auto-generated method stub
		String msg="";
		SqlSession session = sqlmapper.openSession();
		try {
			msg= "Resultado: "+session.insert("hcxml.modificaHC",obj);
			session.commit();
		} catch (Exception e) {
			e.getMessage();
			session.rollback();
		}finally{
			session.close();
		}
	}

}
